package com.maddox.maddoxppjoke.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;
import com.maddox.maddoxppjoke.R;
import com.maddox.maddoxppjoke.model.BottomBar;
import com.maddox.maddoxppjoke.util.AppConfig;

import java.util.List;

/**
 * 重写底部导航按钮
 */
public class AppBottomBar extends BottomNavigationView {
    //菜单图标
    private int[] iconIds = {R.mipmap.icon_tab_home, R.mipmap.icon_tab_sofa, R.mipmap.icon_tab_publish, R.mipmap.icon_tab_find, R.mipmap.icon_tab_mine};

    private BottomBar bottomBar;

    public AppBottomBar(@NonNull Context context) {
        this(context, null);
    }

    public AppBottomBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("RestrictedApi")
    public AppBottomBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        bottomBar = AppConfig.getBottomBar();
        //给按钮定义两种状态,选中和未选中
        int[][] states = new int[2][];
        states[0] = new int[]{android.R.attr.state_selected};
        states[1] = new int[]{};
        //给两种状态的按钮定义对应的颜色,选中状态的颜色和未选中状态颜色
        int[] colors = new int[]{Color.parseColor(bottomBar.activeColor), Color.parseColor(bottomBar.inActiveColor)};
        ColorStateList colorStateList = new ColorStateList(states, colors);
        //把状态和对应的色值设置给按钮以及对应的文本,进行着色
        setItemIconTintList(colorStateList);
        setItemTextColor(colorStateList);
        //设置按钮和文本是否同时显示的状态
        setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
        List<BottomBar.Tab> tabs = bottomBar.tabs;
        for (BottomBar.Tab tab : tabs) {
            if (!tab.enable) {
                continue;
            }
            int itemId = getItemId(tab.pageUrl);
            if (itemId<0){
                return;
            }
            //添加到Menu菜单中
            MenuItem menuItem = getMenu().add(0, itemId, tab.index, tab.title);
            menuItem.setIcon(iconIds[tab.index]);

        }
        //因为每次添加按钮都会重新remove进行排序,所以只有当添加按钮完成后才能进行设置按钮大小
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) getChildAt(0);
        for (int i = 0; i < tabs.size(); i++) {
            BottomBar.Tab tab = tabs.get(i);
            int iconSize = dp2px(tab.size);
            BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(i);
            itemView.setIconSize(iconSize);
            //当没有标题时 也就是中间的大按钮,进行重新设置图标着色
            if (TextUtils.isEmpty(tab.title)){
                itemView.setIconTintList(ColorStateList.valueOf(Color.parseColor(tab.tintColor)));
                //去除选中上下浮动效果
                itemView.setShifting(false);
            }

        }


    }
   //dp转px
    private int dp2px(int size) {
        return (int) (getContext().getResources().getDisplayMetrics().density*size+0.5f);
    }

    //要与Destination中的页面id对应
    private int getItemId(String pageUrl) {

        return AppConfig.getDestConfig().get(pageUrl).getId();

    }
}
